// pages/login/login.js
const app = getApp()
import API from "../../api/index";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: '',
    name: '', // 名字
    psw: '', // 密码
    saveBtn: false,//保存按钮
    userinfo:{
      nickname:'',
      avatar:''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },
  bindGetUserInfo: function(e) {
    console.log(e.detail.userInfo)
    if (e.detail.userInfo){
      this.setData({
        ['userinfo.nickname']: e.detail.userInfo.nickName,
        ['userinfo.avatar']:e.detail.userInfo.avatarUrl
      })
      this.save()
    } else {
      this.save()
      // wx.navigateTo({
      //   url: "/pages/index/index"
      // })
    }
  },
  _updateUser(){
    if(this.data.userinfo.avatar!=''&&this.data.userinfo.nickname!=''){
      let obj = {
        nickname: this.data.userinfo.nickname,
        avatar: this.data.userinfo.avatar
      }
      API.updateUser(obj).then(res=>{
        this._getUserInfo()
      })
    }
  },
  // 监听名字
  bindName: function (e) {
    console.info(e)
    if (e.detail.value == '') {
      this.setData({
        saveBtn: false
      })
    }
    this.setData({
      name: e.detail.value
    })
    
    app.globalData.username = e.detail.value
    if (this.data.name != '' && this.data.psw != '') {
      this.setData({
        saveBtn: true
      })
    } else {
      this.setData({
        saveBtn: false
      })
    }
  },
  // 监听密码
  bindPsw: function (e) {
    if (e.detail.value == '') {
      console.info('kongle ')
      this.setData({
        saveBtn: false
      })
    }
    this.setData({
      psw: e.detail.value
    })

    app.globalData.password = e.detail.value
    if (this.data.name != '' && this.data.psw != '') {
      this.setData({
        saveBtn: true
      })
    } else {
      this.setData({
        saveBtn: false
      })
    }
  },

  // 保存
  save: function () {
    let _that = this
    if(_that.data.name != '' && _that.data.psw != ''){
      let name = _that.data.name
      let psw = _that.data.psw
      API.login({username:name,password:psw}).then(res=>{
        // app.globalData.access_token = res.data.access_token
        // app.globalData.refresh_token = res.data.refresh_token
        wx.setStorageSync('access_token',  res.data.access_token)
        wx.setStorageSync('refresh_token',  res.data.refresh_token)
        // this._updateUser()
        if(this.data.userinfo.avatar!=''&&this.data.userinfo.nickname!=''){
          this._updateUser()
        }else{
          wx.switchTab({
            url: "/pages/index/index"
          })
          // wx.navigateTo({
          //   url:"/pages/document/index",
          // })
        }
        
      })
    }
  },
  //获取用户信息-用户权限
  _getUserInfo(){
    API.getUserInfo().then(res=>{
      app.globalData.userInfo = res.data
      wx.setStorageSync('userInfo', res.data)
      wx.switchTab({
        url: "/pages/index/index"
      })
      // wx.navigateTo({
      //   url:"/pages/document/index",
      // })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(wx.getStorageSync('access_token')!=''){
      wx.switchTab({
        url:  "/pages/index/index",
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})